package com.gticket.buyticket.handler;

import com.gticket.buyticket.pojo.FilmSchedule;
import com.gticket.buyticket.service.FilmScheduleService;
import lombok.Data;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Data
@RestController
public class FilmScheduleHandler {

    @Resource
   private FilmScheduleService filmScheduleService;

    //单个电影所有排片计划
	@CrossOrigin(origins = "*")
    @GetMapping("/filmSchedule")
    public List<FilmSchedule> getFilmSchedlue(FilmSchedule filmSchedule){
        System.out.println("_______________________________________________________________");
        System.out.println("filmSchedlue+++++++++++++++++++++++++++++++++++++++"+filmSchedule);
        System.out.println("-----------------------------------------++++++++++"+filmScheduleService.getFilmSchedule(filmSchedule));
        return filmScheduleService.getFilmSchedule(filmSchedule);
    }


    //仅仅只查询电影要上映的所有日期
	@CrossOrigin(origins = "*")
    @GetMapping("/showOneFilmDate")
    public List<String> getFilmDate(FilmSchedule filmSchedule) throws ParseException {
        //日期同一天要去重
        List<String> filmDate = filmScheduleService.getFilmDate(filmSchedule);
        for  ( int  i  =   0 ; i  <  filmDate.size()  -   1 ; i ++ )  {
            for  ( int  j  =  filmDate.size()  -   1 ; j  >  i; j -- )  {
                if  (filmDate.get(j).equals(filmDate.get(i)))  {
                    filmDate.remove(j);
                }
            }
        }
        //获取当日日期
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        //循环比较日期大小，小于当天的日期不予显示在页面上
        for (int i= 0 ;i < filmDate.size()  -   1 ; i ++ )  {
            //String格式转为Date格式
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date tempDatei = dateFormat.parse(filmDate.get(i));
            //与当天日期相比较，小于当天日期移除出集合
            int compareTo = tempDatei.compareTo(date);
            if(compareTo==-1){
                filmDate.remove(i);
            }
        }
        System.out.println("newDate1:++++++++++++++++++++++++++++++++"+filmDate);
        //日期大小冒泡排序
//        for (int i= 0 ;i < filmDate.size() - 1; i ++ )  {
//            for (int j= 0 ;j < filmDate.size()  -   1 - i; j ++ )  {
//
//                if(filmDate.get(j).compareTo(filmDate.get(i))==-1){
//                    System.out.println("newDatei111:++++++++++++++++++++++++++++++++"+filmDate.get(i));
//                    System.out.println("newDatej222:++++++++++++++++++++++++++++++++"+filmDate.get(j));
//                    String temp = filmDate.get(j);
//                    filmDate.set(j,filmDate.get(i));
//                    filmDate.set(i,temp);
//                    System.out.println("newDatei111111:++++++++++++++++++++++++++++++++"+filmDate.get(i));
//                    System.out.println("newDatej222222:++++++++++++++++++++++++++++++++"+filmDate.get(j));
//                }
//            }
//        }
        System.out.println("newDate3:++++++++++++++++++++++++++++++++"+filmDate);
        return filmDate;
    }

    //单个电影的单个排片计划
	@CrossOrigin(origins = "*")
    @GetMapping("/showOneFilmSchedule")
    public FilmSchedule getOneFilmSchedlue(FilmSchedule filmSchedule){
        return filmScheduleService.getOneFilmSchedule(filmSchedule);
    }

}
